package com.gpy.hospital.cms.controller;

import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.pojo.Book;
import com.gpy.hospital.cms.pojo.BookCount;
import com.gpy.hospital.cms.pojo.User;
import com.gpy.hospital.cms.service.IBookCountService;
import com.gpy.hospital.cms.service.IBookService;
import com.gpy.hospital.cms.vo.BookVo;
import com.gpy.hospital.common.Const;
import com.gpy.hospital.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author
 * @Date 2020/3/27 14:42
 * @Desc
 **/
@Controller
public class BookController {
    @Autowired
    private IBookService bookService;
    @Autowired
    private IBookCountService bookCountService;

    @GetMapping("/reception/book")
    public String receptionBook(HttpServletRequest request, @RequestParam("id") Integer id) {
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "redirect:/reception/login/page";
        }
        BookCount byEmail = bookCountService.getByEmail(user.getEmail());
        if (null!= byEmail) {
            if (byEmail.getCount() > 2) {
                return "/reception/myBookCount";
            }
        }
        return bookService.book(user, id);

    }

    @GetMapping("/reception/my/book")
    public String myBook(HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        List<BookVo> books = bookService.getByEmail(user.getEmail());
        model.addAttribute("books", books);
        return "/reception/myBook";
    }

    @GetMapping("/reception/book/cancel")
    public String cancelBook(HttpServletRequest request, @RequestParam("id") Integer id) {
        // 记录次数
        BookCount bookCount = bookCountService.getByEmail(((User) request.getSession().getAttribute(Const.CURRENT_USER)).getEmail());
        if (null == bookCount) {
            // 插入数据
            bookCountService.insert(((User) request.getSession().getAttribute(Const.CURRENT_USER)).getEmail(), 0);
        } else {
            // 更新数据
            bookCountService.update(((User) request.getSession().getAttribute(Const.CURRENT_USER)).getEmail(), bookCount.getCount() + 1);
        }
        bookService.deleteById(id);
        return "/reception/cancelBook";
    }

    @GetMapping("/yu/yue/doctor")
    public String yuYueDoctor() {
        return "/cms/yuYueList";
    }


    @GetMapping("/cms/yu/yue/list")
    @ResponseBody
    public ServerResponse userList(HttpServletRequest request,
                                   @RequestParam(value = "email", defaultValue = "") String email,
                                   @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        PageInfo<Book> pageInfo = bookService.getByDoctorId(user.getId(), email, pageNum, pageSize);
        return ServerResponse.createBySuccess("查询成功！", pageInfo.getList(), pageInfo.getTotal());
    }

    @GetMapping("/cms/yu/yue/cancel")
    @ResponseBody
    public ServerResponse yuYueCancel(@RequestParam("id") Integer id) {
        return bookService.deleteId(id);
    }

}
